package com.app.controller;

/*
  @author 阿伟
  @create 2020-11-23:14:53
  用户Controller
*/

import com.app.condition.UserCondition;
import com.app.dao.ConsultDao;
import com.app.dao.UserDao;
import com.app.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.ResponseResult;
import com.common.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    UserService userService;

    @Autowired
    UserDao userDao;

    @Autowired
    ConsultDao consultDao;

    @ApiOperation(value = "通过ID查询用户")
    @GetMapping("/{userId}")
    public ResponseResult getUserById(@ApiParam(name = "userId",value = "用户ID",required = true)
            @PathVariable("userId") String userId){
        User user = userDao.selectById(userId);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("user",user);
    }

    @ApiOperation(value = "分页查询用户")
    @GetMapping("/{pageIndex}/{pageSize}")
    public ResponseResult getUserByLimit(
            @ApiParam(name = "pageIndex",value = "当前页",required = true)
            @PathVariable("pageIndex") String pageIndex,
            @ApiParam(name = "pageSize",value = "页大小",required = true)
            @PathVariable("pageSize") String pageSize,
            @ApiParam(name = "userCondition",value = "查询的条件",required = false)
            UserCondition userCondition){
        Long index = Long.parseLong(pageIndex);
        Long size = Long.parseLong(pageSize);
        //page对象
        Page<User> page = new Page<>(index,size);
//        userDao.selectPage(page,null);
        //调用查询的方法
        userService.selectUserByLimit(page,userCondition);
        List<User> users = page.getRecords();
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("user",users);
    }

    //用户删除帖子
//    @ApiOperation(value = "用户删帖")
//    @GetMapping("/{userName}")
//    public ResponseResult deleteConsultByUserName(
//            //required = true 表示必填
//            @ApiParam(name = "userName",value = "用户昵称",required = true)
//            @PathVariable("userName") String userName){
//        //条件构造器
//        QueryWrapper<Consult> queryWrapper = new QueryWrapper<>();
//        //添加条件
//        queryWrapper.eq("user_nickname",userName);
//        List<Consult> consults = consultDao.selectList(queryWrapper);
//        //判断帖子是否是当前登录的用户所发
//        if(consults != null){
//            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("opr","ok");
//        }
//        return ResponseResult.error(Code.REQUEST_ERROR).Data("opr","不是发的");
//    }

    //登录验证  /{userName}/{password}
//    @ApiOperation(value = "用户登录验证")
//    @GetMapping("/login")
//    public ResponseResult login(
//            @ApiParam(name = "phone",value = "手机号",required = true)
//            @RequestParam(value = "phone") String phone,
//            @ApiParam(name = "password",value = "密码",required = true)
//            @RequestParam(value = "password") String password){
//        //查询条件
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_phone",phone);
//        queryWrapper.eq("user_password",password);
//        User user = userDao.selectOne(queryWrapper);
//        if(user != null){
//            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("user",user);
//        }else {
//            return ResponseResult.error(Code.REQUEST_ERROR).Data("user",null);
//        }
//    }

    //注册
    @ApiOperation(value = "用户注册")
    @GetMapping("/register")
    public ResponseResult register(
            @ApiParam(name = "phone",value = "手机号",required = true)
            @RequestParam(value = "phone") String phone,
            @ApiParam(name = "nickname",value = "昵称",required = true)
            @RequestParam(value = "nickname") String nickname,
            @ApiParam(name = "password",value = "密码",required = true)
            @RequestParam(value = "password") String password,
            @ApiParam(name = "yzm",value = "验证码",required = true)
            @RequestParam(value = "yzm") String yzm){

      return userService.userRegister(phone,nickname,password,yzm);
    }

    @ApiOperation(value = "发送验证码")
    @GetMapping("/sendSMS")
    public ResponseResult sendSMS(@RequestParam("phone") String phone){
        return userService.sendSMS(phone);
    }

    @ApiOperation(value = "修改头像")
    @PostMapping("/upload")
    public ResponseResult upload(
            @RequestParam("multipartFile") MultipartFile multipartFile,
            @ApiParam(name = "phone",value = "账号",required = true)
            @RequestParam("phone") String phone){
            return userService.upload(multipartFile,phone);
    }

    @ApiOperation(value = "修改密码")
    @GetMapping("/modifyPass")
    public ResponseResult modifyPass(
            @ApiParam(name = "phone",value = "账号",required = true)
            @RequestParam("phone") String phone,
            @ApiParam(name = "password",value = "新密码",required = true)
            @RequestParam("password") String password,
            @ApiParam(name = "yzm",value = "验证码",required = true)
            @RequestParam(value = "yzm") String yzm){
        System.out.println(phone);
        System.out.println(password);
        System.out.println(yzm);
        return userService.modifyPass(phone,password,yzm);
    }

    @ApiOperation(value = "根据id查询订单")
    @GetMapping("/getorder")
    public ResponseResult getorderByPhone(@RequestParam("phone") String phone){
        return userService.getOrderByPhone(phone);
    }

    @ApiOperation(value = "根据手机号查询头像")
    @GetMapping("/gethead")
    public ResponseResult getHeadByPhone(@RequestParam("phone") String phone){
        return userService.getHeadByPhone(phone);
    }

    @ApiOperation(value = "根据手机号修改昵称")
    @GetMapping("/modifynickname")
    public ResponseResult modifyNickname(
            @ApiParam(name = "nickname",value = "昵称",required = true)
            @RequestParam("nickname") String nickname,
            @ApiParam(name = "phone",value = "账号",required = true)
            @RequestParam("phone") String phone){

        return userService.modifyNickname(nickname, phone);
    }

    @ApiOperation(value = "根据手机号查询昵称")
    @GetMapping("/getnickname")
    public ResponseResult getNickname(
            @ApiParam(name = "phone",value = "账号",required = true)
            @RequestParam("phone") String phone){
        return userService.getNickByPhone(phone);
    }
}
